package cn.bugstack.xfg.dev.tech.test;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

@Slf4j
public class RedisConnectionTest {

    @Test
    public void testRedisConnection() {
        RedissonClient redissonClient = null;
        try {
            // 创建Redis配置
            Config config = new Config();
            config.useSingleServer()
                    .setAddress("redis://117.72.151.140:16379")
                    .setConnectionPoolSize(10)
                    .setConnectionMinimumIdleSize(5)
                    .setIdleConnectionTimeout(30000)
                    .setConnectTimeout(5000)
                    .setRetryAttempts(3)
                    .setRetryInterval(1000)
                    .setPingConnectionInterval(60000)
                    .setKeepAlive(true);

            // 创建Redis客户端
            redissonClient = Redisson.create(config);
            
            // 测试连接
            String testKey = "test:connection:" + System.currentTimeMillis();
            String testValue = "Hello Redis!";
            
            // 写入测试数据
            redissonClient.getBucket(testKey).set(testValue);
            log.info("成功写入Redis数据: {} = {}", testKey, testValue);
            
            // 读取测试数据
            String retrievedValue = (String) redissonClient.getBucket(testKey).get();
            log.info("成功读取Redis数据: {} = {}", testKey, retrievedValue);
            
            // 删除测试数据
            redissonClient.getBucket(testKey).delete();
            log.info("成功删除Redis测试数据: {}", testKey);
            
            // 测试ping
            boolean pingResult = redissonClient.getNodesGroup().pingAll();
            log.info("Redis Ping测试结果: {}", pingResult ? "成功" : "失败");
            
            log.info("✅ Redis连接测试成功!");
            
        } catch (Exception e) {
            log.error("❌ Redis连接测试失败: {}", e.getMessage(), e);
        } finally {
            if (redissonClient != null) {
                try {
                    redissonClient.shutdown();
                    log.info("Redis客户端已关闭");
                } catch (Exception e) {
                    log.error("关闭Redis客户端时出错: {}", e.getMessage());
                }
            }
        }
    }
    
    @Test
    public void testRedisConnectionSimple() {
        try {
            // 简单的连接测试
            Config config = new Config();
            config.useSingleServer()
                    .setAddress("redis://117.72.151.140:16379")
                    .setConnectTimeout(3000)
                    .setRetryAttempts(1);

            RedissonClient client = Redisson.create(config);
            
            // 简单ping测试
            boolean pingResult = client.getNodesGroup().pingAll();
            log.info("Ping结果: {}", pingResult);
            log.info("✅ Redis简单连接测试成功!");
            
            client.shutdown();
            
        } catch (Exception e) {
            log.error("❌ Redis简单连接测试失败: {}", e.getMessage());
            
            // 分析可能的原因
            if (e.getMessage().contains("timeout")) {
                log.error("可能原因: 网络超时，请检查网络连接和防火墙设置");
            } else if (e.getMessage().contains("refused")) {
                log.error("可能原因: Redis服务未启动或端口被占用");
            } else if (e.getMessage().contains("UnknownHost")) {
                log.error("可能原因: 主机地址无法解析");
            }
        }
    }
}
